import {createWebHashHistory, createRouter, RouteRecordRaw} from "vue-router";
import routes from "./routes";
import { useAppStore } from "@/store/app";

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

// 导航守卫：验证 Token
router.beforeEach(async (to, from, next) => {
    const appStore = useAppStore()
    const token = appStore.getToken
    // 检查本地是否存在 Token
    if (!token) {
        if (to.name == 'login'){
            next()
            return
        }else{
            // 如果没有 Token，跳转到登录页面
            next({name: 'login'});
            return;
        }
    } else {
        if (to.name == 'login'){
            next({
                path: '/',
            })
            return
        }
        // 不需要验证 Token，直接继续导航
        next();
    }
});

export default router